//
//  CYZijinrongWebView.m
//  CreditAddressBook
//
//  Created by mosu on 2016/10/12.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYZijinrongWebView.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "CYCardIDCheckVC.h"
#import "MBProgressHUD+LE.h"
#import "UIAlertView+Block.h"
#import "CYShimingbangkaVC.h"

@interface CYZijinrongWebView ()<UIWebViewDelegate>

@property (nonatomic, strong)UIWebView *webview;
@property (nonatomic, strong)UIView *hiview;
@property (nonatomic, assign)BOOL isfirstLoad;
@property (nonatomic, strong)NSString *url1;

@end

@implementation CYZijinrongWebView

- (void)viewDidLoad {
    [super viewDidLoad];
     
     self.navigationController.interactivePopGestureRecognizer.enabled = NO;
     self.title  = @"自金融";
     //[self.navigationBar setBackgroundColor:[UIColor blackColor]];
     CYUser *user = [CYLoginHttpTool userData].user;
     self.view.backgroundColor = [UIColor blackColor];
     _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen ]bounds].size.width,[[UIScreen mainScreen ]bounds].size.height)];
     _webview.backgroundColor = CYColor(68, 68, 68, 1);
     _webview.delegate = self;
     
     //测试：自金融  http://192.168.0.200:8100/index.html#/userAuth/%@/%@/2     http://zjr.credit-manage.com/index.html#/userAuth/%@/%@/2    http://192.168.0.102:8100/#/tab/help

     if (user) {
          NSString *url= [NSString stringWithFormat:@"http://zjr.credit-manage.com/index.html#/userAuth/%@/%@/2",user.userId,user.token];
          
//         NSString *urltesr = @"http://192.168.0.102:8100/#/tab/help";
          
          NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
          [_webview loadRequest:request];
          
     }
     //添加滑动返回
     UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
     rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
     [_webview addGestureRecognizer:rightSwipeGestureRecognizer];
     // Do any additional setup after loading the view.
     [self.view addSubview:_webview];
     self.isfirstLoad = YES;

    // Do any additional setup after loading the view.
}



-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
//     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//          if (buttonIndex == 0) {
//               [[self navigationController] setNavigationBarHidden:NO animated:NO];
//               [self.navigationController popViewControllerAnimated:YES];
//               
//          }
//     }];
     
}

/**
 *  添加滑动返回
 */
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
     if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
          [[self navigationController] setNavigationBarHidden:NO animated:NO];
          [self.navigationController popViewControllerAnimated:YES];
     }
}

- (void)report:(NSArray *)params{
     
      NSLog(@"%@", params);

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
     
     [self.view addSubview:_hiview];
     NSString *url = [[request URL] absoluteString];
     NSLog(@"url = %@",url);
     //h5内返回控件字段方法:loanlist
     if([url hasSuffix:@"loanlist"]) {
          dispatch_async(dispatch_get_main_queue(), ^{
               //返回上一级时，取消nav的隐藏
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               //js交互oc获取到h5内的返回按钮后，点击返回，popvc
               [self.navigationController popViewControllerAnimated:YES];
          });
          return NO;
     }
     
     if([url rangeOfString:@"report"].location !=NSNotFound)//_roaldSearchText
     {
          NSString *str = url;
          CYLog(@"str = %@",str);
         NSString *str1 = [str substringFromIndex:12];//截取掉下标11之后的字符串
          CYLog(@"str1 = %@",str1);
          if (str1 && !str1.isEmpty) {
               
               UIPasteboard *pab = [UIPasteboard generalPasteboard];
               [pab setString:str1];
               if (pab == nil) {
                    [MBProgressHUD showError:@"复制失败"];
                    
               }else
               {
                    [MBProgressHUD showSuccess:@"已复制"];
                    
               }

               
          }
     }
     else
     {
          NSLog(@"no");
     }
     
     
     if ([url hasSuffix:@"jumpCertification"]) {
               dispatch_async(dispatch_get_main_queue(), ^{
                    
                    NSMutableArray * array =[[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
                    //删除最后一个，也就是自己
                    [array removeObjectAtIndex:array.count-1];
                    
                    CYCardIDCheckVC *  controller =[[CYCardIDCheckVC alloc]initWithNibName:nil bundle:nil];
                    [array addObject:controller];
                    [[self navigationController]setNavigationBarHidden:NO animated:NO];
                    controller.hidesBottomBarWhenPushed = YES;
                    [self.navigationController setViewControllers:array animated:YES];
                    
               });
          
               return NO;
     
          }
     
     return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
